package com.navcoo.todolist.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.navcoo.todolist.mapper.TodoDataMapper;
import com.navcoo.todolist.pojo.dao.TodoDataDAO;
import com.navcoo.todolist.service.CreateTodoData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * 创建待办事项数据导入
 *
 * @author Navcoo
 * @date 2024-02-13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CreateTodoDataImpl extends ServiceImpl<TodoDataMapper, TodoDataDAO> implements CreateTodoData {

    private final TodoDataMapper todoDataMapper;

    /**
     * 创建待办事项数据
     *
     * @param title 标题
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject createTodoData(String title) {

        // 获取当前时间为创建时间
        LocalDateTime createTime = LocalDateTime.now();
        log.info("创建待办事项数据，待办事项标题为：{}，待办完成状态：{}，创建时间为：{}", title, false, createTime);

        // 创建待办事项数据
        TodoDataDAO todoDataDAO = new TodoDataDAO();
        todoDataDAO.setTitle(title);
        todoDataDAO.setIsDone(false);
        todoDataDAO.setCreatedAt(createTime);
        todoDataDAO.setUpdatedAt(createTime);
        todoDataDAO.setIsDelete(false);
        log.info("创建待办事项数据，待办事项为：{}", todoDataDAO);

        // 插入数据
        todoDataMapper.insert(todoDataDAO);
        // 获取待办事项ID
        String instId = todoDataDAO.getInstId();

        // 创建返回结果
        JSONObject result = new JSONObject();

        if (instId.isEmpty()) {
            log.info("创建待办事项数据失败");
        } else {
            // 成功
            log.info("创建待办事项数据成功");
            result.set("instId", instId);
        }

        log.info("返回结果：{}", result);
        return result;
    }

}
